/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.hazard.setting.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.constant.CommonConstant;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.hazard.common.constant.enums.HazardSysDictEnum;
import com.hopes.bizz.hazard.setting.dto.HazBasicFactorConfigExcelDTO;
import com.hopes.bizz.hazard.setting.entity.HazBasicFactorConfigEntity;
import com.hopes.bizz.hazard.setting.mapper.HazBasicFactorConfigMapper;
import com.hopes.bizz.hazard.setting.service.HazBasicFactorConfigService;
import com.hopes.bizz.hazard.setting.vo.HazBasicFactorConfigExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * 监控因子设置
 *
 * @author daijie
 * @date 2023-10-31 16:52:06
 */
@Service
public class HazBasicFactorConfigServiceImpl extends ServiceImpl<HazBasicFactorConfigMapper, HazBasicFactorConfigEntity> implements HazBasicFactorConfigService {

    @Override
    public ValidateImportBO<HazBasicFactorConfigExcelDTO> validateImport(List<HazBasicFactorConfigExcelDTO> excelDTOList) {
        ValidateImportBO<HazBasicFactorConfigExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<HazBasicFactorConfigExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        for (HazBasicFactorConfigExcelDTO hazBasicFactorConfigExcelDTO : excelDTOList) {
            final String validateRes = ValidatorUtils.validateEntity(hazBasicFactorConfigExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(hazBasicFactorConfigExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(hazBasicFactorConfigExcelDTO.getLineNum(), new HashSet<>() {
                    private static final long serialVersionUID = -7630198182668131428L;
                    {
                        add(validateRes);
                    }
                }));
            }
        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public HazBasicFactorConfigEntity importDtoToEntity(HazBasicFactorConfigExcelDTO excelDto) {
        HazBasicFactorConfigEntity entity= new HazBasicFactorConfigEntity();
        BeanUtils.copyProperties(excelDto, entity);

        if (StrUtil.isNotEmpty(excelDto.getOrgIdStr())) {
            entity.setOrgId(DictResolver.getItemValueByLabel(AdminDictEnum.sys_company, excelDto.getOrgIdStr(), Long::valueOf));
        }
        if (StrUtil.isNotEmpty(excelDto.getModuleCodeStr())) {
            entity.setModuleCode(DictResolver.getItemValueByLabel(HazardSysDictEnum.park_factor_module, excelDto.getModuleCodeStr()));
        }
        if (StrUtil.isNotEmpty(excelDto.getFactorTypeStr())) {
            entity.setFactorType(DictResolver.getItemValueByLabel(HazardSysDictEnum.park_factor_type, excelDto.getFactorTypeStr()));
        }
        if (StrUtil.isNotEmpty(excelDto.getUseFlagStr())) {
            entity.setUseFlag(DictResolver.getItemValueByLabel(SysDictEnum.yes_no_type, excelDto.getUseFlagStr()).equals(CommonConstant.boolean_true_string));
        }
        return entity;
    }

    @Override
    public HazBasicFactorConfigExcelVO exportEntityToVo(HazBasicFactorConfigEntity entity) {
        HazBasicFactorConfigExcelVO excelVO = new HazBasicFactorConfigExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setOrgIdStr(entity.getOrgIdStr());
        excelVO.setUseFlagStr(entity.getUseFlagStr());
        excelVO.setFactorTypeStr(entity.getFactorTypeStr());
        excelVO.setModuleCodeStr(entity.getModuleCodeStr());
        return excelVO;
    }
}
